<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]>      <html class="no-js"> <!--<![endif]-->
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title></title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="" />
  </head>
  <body>
    <!--[if lt IE 7]>
      <p class="browsehappy">
        You are using an <strong>outdated</strong> browser. Please
        <a href="#">upgrade your browser</a> to improve your experience.
      </p>
    <![endif]-->

    <script>
      const mydata = [
        {
          children: [
            {
              children: [
                {
                  children: [
                    {
                      unitName: "jej",
                      id: "0e2778c297c1483d8380a2611b04198f",
                      unitType: 3,
                      sort: 1,
                      parentId: "b24463f8cf364b0ebcf1e69b2646e4d4",
                    },
                  ],
                  unitName: "测试4",
                  id: "b24463f8cf364b0ebcf1e69b2646e4d4",
                  unitType: 2,
                  sort: 1,
                  parentId: "eea4522e5d5c45a08573ad788bd50615",
                },
                {
                  unitName: "d",
                  id: "788fcee7b5fc4c4f9cdad5f46b67cc3a",
                  unitType: 0,
                  sort: 2,
                  parentId: "eea4522e5d5c45a08573ad788bd50615",
                },
                {
                  unitName: "eme",
                  id: "cf9e3e4fcac64b51bc6fc3a2231a6ddc",
                  unitType: 2,
                  sort: 3,
                  parentId: "eea4522e5d5c45a08573ad788bd50615",
                },
                {
                  unitName: "cc",
                  id: "6c46e31b66f64f188105e906913f1117",
                  unitType: 0,
                  sort: 4,
                  parentId: "eea4522e5d5c45a08573ad788bd50615",
                },
                {
                  unitName: "12313131",
                  id: "ec601c79161545fab2d3c7214164a71e",
                  unitType: 1,
                  sort: 5,
                  parentId: "eea4522e5d5c45a08573ad788bd50615",
                },
              ],
              unitName: "测试3",
              id: "eea4522e5d5c45a08573ad788bd50615",
              unitType: 0,
              sort: 1,
              parentId: "2c01f155772946c4aec941ea3ac665fe",
            },
          ],
          unitName: "测试2",
          id: "2c01f155772946c4aec941ea3ac665fe",
          unitType: 1,
          sort: 2,
          parentId: "-1",
        },
        {
          children: [
            {
              children: [
                {
                  unitName: "测试3",
                  id: "3eefb80b958b4da5853b02f662856b24",
                  unitType: 2,
                  sort: 2,
                  parentId: "0c1fb3485ebe41d9b0ef5ba5a4b9879c",
                },
                {
                  unitName: "xxc",
                  id: "36a6a5ee5bac4ea8abb7c8562f1b7d3a",
                  unitType: 3,
                  sort: 3,
                  parentId: "0c1fb3485ebe41d9b0ef5ba5a4b9879c",
                },
              ],
              unitName: "机构2",
              id: "0c1fb3485ebe41d9b0ef5ba5a4b9879c",
              unitType: 0,
              sort: 1,
              parentId: "e2163e75e73341a3a5f6cc1b640dfa74",
            },
            {
              unitName: "机构2",
              id: "1eb4571707a640a1b44f3f95be297759",
              unitType: 0,
              sort: 1,
              parentId: "e2163e75e73341a3a5f6cc1b640dfa74",
            },
            {
              unitName: "机构2",
              id: "cc01125bdc0e4ae4b34903d2781326b2",
              unitType: 0,
              sort: 1,
              parentId: "e2163e75e73341a3a5f6cc1b640dfa74",
            },
            {
              unitName: "机构2",
              id: "bd8e7a3baa27421d855823f378ab5536",
              unitType: 0,
              sort: 2,
              parentId: "e2163e75e73341a3a5f6cc1b640dfa74",
            },
          ],
          unitName: "机构1",
          id: "e2163e75e73341a3a5f6cc1b640dfa74",
          unitType: 0,
          sort: 1,
          parentId: "-1",
        },
      ];
      // 树形结构找到值
      function treeFind(data, key, value) {
        for (let i = 0; i < data.length; i++) {
          if (data[i][key] == value) {
            return data[i];
          }
          if (data[i].children) {
           // 找到了值，就不在继续往下走了，直接退出递归
           let val = treeFind(data[i].children, key, value);
           if(val) return val
          }
        }
      }

      console.log(treeFind(mydata, "id", "3eefb80b958b4da5853b02f662856b24"));
    </script>
  </body>
</html>
